package com.litong.litongjiaoyu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.controller.WxPushServiceQcl;
import com.litong.litongjiaoyu.dao.OrderDao;
import com.litong.litongjiaoyu.entity.SysOrder;
import com.litong.litongjiaoyu.entity.WxMaTemplateData;
import com.litong.litongjiaoyu.entity.WxMssVo;
import com.litong.litongjiaoyu.service.OrderService;
import com.litong.litongjiaoyu.service.PositionService;
import com.litong.litongjiaoyu.utils.IdSequenceUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName OrderServiceImpl
 * @Description 订单系统服务层具体实现
 * @Author jingz
 * @Date 2019/12/29 1:30
 * @Version 1.0
 **/
@Log4j2
@Service
public class OrderServiceImpl implements OrderService {

    /**
     * 引入orderdao
     */
    @Autowired
    private OrderDao orderDao;
    @Resource
    private PositionService position;

    private final String Template_id = "83C_jGnmxdoECGA4Tm39Wi4ZHY4i9APemnnD4NnfIyY";

    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;

    /**
     * 引入订单编号生成工具类
     */
    private IdSequenceUtils idSequenceUtils = new IdSequenceUtils();

    /**
     * 获取该校区的所有订单
     *
     * @param campus
     * @return
     */
    @Override
    public List<SysOrder> query(String campus) {
        return orderDao.query(campus);
    }
    
    @Override
    public List<SysOrder> getPurchaseHistory(String phone) {
        return orderDao.getPurchaseHistory(phone);
    }
    
    /**
     * 根据订单号获取当前订单信息
     *
     * @param ID
     * @return
     */
    @Override
    public SysOrder getOrderById(String ID) {
        return orderDao.getOrderById(ID);
    }

    /**
     * 添加订单信息
     *
     * @param sysOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addOrder(SysOrder sysOrder) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));

        String time = simpleDateFormat.format(new Date());


        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

//      预约人
        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(sysOrder.getName());
        data.put("name1", wxMaTemplateData1);

//      预约时间
        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue(time);
        data.put("date2", wxMaTemplateData2);

//      预约课程
        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();
        wxMaTemplateData3.setValue(sysOrder.getCourse());
        data.put("thing3", wxMaTemplateData3);
//      门店地址
        WxMaTemplateData wxMaTemplateData4 = new WxMaTemplateData();
        wxMaTemplateData4.setValue(sysOrder.getCampus());
        data.put("thing4", wxMaTemplateData4);

//      设置接收人、模板id，appid，appSecret
        WxMssVo wxMssVo = new WxMssVo();

        sysOrder.setId(Long.toString(idSequenceUtils.nextId()));



        java.sql.Timestamp timestamp = new Timestamp(simpleDateFormat.parse(time).getTime());

        sysOrder.setDateTime(timestamp);

        System.out.println(sysOrder.toString());

        int a = orderDao.addOrder(sysOrder);

        if (a > 0){
            //获取拥有该权限的人的openid
            List<String> openids = position.getOpenId("订单中心");


            for (String openid : openids) {
                wxMssVo.setTouser(openid);

                wxMssVo.setPage("pages/order/order");
                wxMssVo.setData(data);
                wxMssVo.setTemplate_id(Template_id);
                //        获取消息发送结果
                JSONObject result = wxPushServiceQcl.pushOneUser("wx6a41faf108b8ecab", "6e9c02ef2d83dcd91a2f0e730a9aa358", wxMssVo);
            }

            return a;

        } else {
            return a;
        }

    }

    /**
     * 修改订单信息
     *
     * @param sysOrder
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateOrder(SysOrder sysOrder){
        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

//      报名项目
        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(sysOrder.getCourse());
        data.put("thing1", wxMaTemplateData1);
        sysOrder.setCourse(null);

//      学生姓名
        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue(sysOrder.getName());
        data.put("name5", wxMaTemplateData2);

        sysOrder.setName(null);

//      备注
        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();

        if(sysOrder.getStatus() == 1) {
            wxMaTemplateData3.setValue("报名成功！请在开课时间前往指定校区报到");
        } else {
            wxMaTemplateData3.setValue("报名失败！若有疑问，请联系该校区前台");
        }
        data.put("thing3", wxMaTemplateData3);

//      设置接收人、模板id，appid，appSecret
        WxMssVo wxMssVo = new WxMssVo();

        int a = orderDao.updateOrder(sysOrder);

        if (a > 0){
//          获取拥有该权限的人的openid
            wxMssVo.setTouser(sysOrder.getOpenId());

            wxMssVo.setPage("pages/index/index");
            wxMssVo.setData(data);
            wxMssVo.setTemplate_id("GQ3OcMyBw2Gi-L5uEOCGzre58gKCk3hYrzuvdD4uTz4");
            //        获取消息发送结果
            JSONObject result = wxPushServiceQcl.pushOneUser("wx78763b2bea2646c2", "571a3836b6b557d2e7dc1aaeabdc0be2", wxMssVo);

            return a;

        } else {
            return a;
        }
    }

    /**
     * 根据订单号删除某个订单
     *
     * @param ID
     * @return
     */
    @Transactional
    @Override
    public int deleteOne(String ID) {
        return orderDao.deleteOne(ID);
    }

    /**
     * 删除所有订单信息
     *
     * @return
     */
    @Transactional
    @Override
    public int deleteAll() {
        return orderDao.deleteAll();
    }
}
